package itheima.zuoye.Test2;
import java.io.*;




public class RunCountValidator {
    private static final String COUNT_FILE = "run_count1.txt";

    public static void main(String[] args) {
        int count = readRunCount();
        if (count >= 3) {
            System.out.println("本软件只能免费使用3次,欢迎您注册会员后继续使用~");
        } else {
            System.out.println("欢迎使用本软件,第" + (count + 1) + "次使用免费~");
            writeRunCount(count + 1);
        }
    }

    /**
     * 读取运行次数
     * @return 运行次数
     */
    private static int readRunCount() {
        int count = 0;
        try (BufferedReader reader = new BufferedReader(new FileReader(COUNT_FILE))) {
            String line = reader.readLine();
            if (line != null) {
                count = Integer.parseInt(line);
            }
        } catch (IOException e) {
            // 如果文件不存在或读取失败，视为第一次运行
        }
        return count;
    }

    /**
     * 写入运行次数
     * @param count 运行次数
     */
    private static void writeRunCount(int count) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(COUNT_FILE))) {
            writer.write(String.valueOf(count));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



